<!DOCTYPE html>
<html>
<head>
<title>Circuit Diagram Fig. 10.1</title>
</head>
<body>
<canvas id="circuitCanvas" width="700" height="450"></canvas>
<script>
    const canvas = document.getElementById('circuitCanvas');
    const ctx = canvas.getContext('2d');

    // Style settings
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    ctx.lineWidth = 2;
    ctx.font = 'bold 20px Arial';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';

    // Helper function to draw a resistor with internal lines
    function drawResistor(x, y, width, height, label, labelPos) {
        // Draw the rectangle
        ctx.strokeRect(x, y - height / 2, width, height);

        // Draw internal lines (3 of them to create 4 sections)
        const numDividers = 3;
        for (let i = 1; i <= numDividers; i++) {
            const lineX = x + (i * width / (numDividers + 1));
            ctx.beginPath();
            ctx.moveTo(lineX, y - height / 2);
            ctx.lineTo(lineX, y + height / 2);
            ctx.stroke();
        }

        // Draw the label
        if (label) {
            if (labelPos === 'above') {
                ctx.fillText(label, x + width / 2, y - height/2 - 18);
            } else { // 'below'
                ctx.fillText(label, x + width / 2, y + height/2 + 18);
            }
        }
    }
    
    // Helper function to draw a switch contact point
    function drawContact(x, y, label) {
        // Draw the filled circle
        ctx.beginPath();
        ctx.arc(x, y, 5, 0, 2 * Math.PI);
        ctx.fill();
        
        // Draw the label
        ctx.fillText(label, x - 25, y);
    }

    // --- Main Drawing ---

    // Define coordinates
    const y_power = 100;
    const y_A = 170;
    const y_B = 240;
    const y_C = 320;
    
    const x_contact = 220;
    const x_pivot = 150;
    const y_pivot = 190;
    
    const res_width = 120;
    const res_height = 20;
    const x_res_start_col1 = 260;
    const x_res_start_col2 = 400;

    const x_junction = 600;
    const y_junction = (y_B + y_C) / 2;

    // 1. Draw Power Supply and Right Wiring
    ctx.beginPath();
    ctx.moveTo(480, y_power); // Right terminal start
    ctx.lineTo(x_junction, y_power);
    ctx.lineTo(x_junction, y_junction);
    ctx.stroke();
    
    // Power supply text label
    ctx.fillText("240 V", 400, y_power - 30);
    
    // Power supply symbol (terminals and tilde)
    ctx.beginPath();
    ctx.arc(480, y_power, 6, 0, 2 * Math.PI, true); // Right terminal
    ctx.stroke();
    ctx.beginPath();
    ctx.arc(320, y_power, 6, 0, 2 * Math.PI, true); // Left terminal
    ctx.stroke();
    ctx.beginPath();
    ctx.moveTo(450, y_power); // Tilde
    ctx.bezierCurveTo(420, y_power - 15, 380, y_power + 15, 350, y_power);
    ctx.stroke();

    // 2. Draw Left Wiring and Switch
    // Wire from left power terminal to switch
    ctx.beginPath();
    ctx.moveTo(320, y_power);
    ctx.lineTo(80, y_power);
    ctx.lineTo(80, 220);
    ctx.lineTo(x_pivot, y_pivot);
    ctx.stroke();
    
    // Switch arm (in position A)
    ctx.beginPath();
    ctx.moveTo(x_pivot, y_pivot);
    ctx.lineTo(x_contact, y_A);
    ctx.stroke();
    
    // Switch contacts
    drawContact(x_contact, y_A, 'A');
    drawContact(x_contact, y_B, 'B');
    drawContact(x_contact, y_C, 'C');

    // 3. Draw Branch B (with resistor X)
    // Wire from contact B
    ctx.beginPath();
    ctx.moveTo(x_contact, y_B);
    ctx.lineTo(x_res_start_col1, y_B);
    ctx.stroke();
    
    // Resistor X
    drawResistor(x_res_start_col1, y_B, res_width, res_height, 'X', 'above');
    
    // Wire from resistor X to junction
    ctx.beginPath();
    ctx.moveTo(x_res_start_col1 + res_width, y_B);
    ctx.lineTo(x_junction, y_junction);
    ctx.stroke();
    
    // 4. Draw Branch C (with resistors Y and Z)
    // Wire from contact C
    ctx.beginPath();
    ctx.moveTo(x_contact, y_C);
    ctx.lineTo(x_res_start_col1, y_C);
    ctx.stroke();
    
    // Resistor Y
    drawResistor(x_res_start_col1, y_C, res_width, res_height, 'Y', 'below');

    // Wire between Y and Z
    ctx.beginPath();
    ctx.moveTo(x_res_start_col1 + res_width, y_C);
    ctx.lineTo(x_res_start_col2, y_C);
    ctx.stroke();

    // Resistor Z
    drawResistor(x_res_start_col2, y_C, res_width, res_height, 'Z', 'below');

    // Wire from resistor Z to junction
    ctx.beginPath();
    ctx.moveTo(x_res_start_col2 + res_width, y_C);
    ctx.lineTo(x_junction, y_junction);
    ctx.stroke();

    // 5. Draw Junction Point on the right
    ctx.beginPath();
    ctx.arc(x_junction, y_junction, 5, 0, 2 * Math.PI);
    ctx.fill();

    // 6. Draw Figure Label
    ctx.fillText("Fig. 10.1", 350, 420);
    
</script>
</body>
</html>